package com.daxilong.starchen.player.api;

import android.content.Context;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.daxilong.starchen.player.utils.Result;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ApiUtils {

    private static final String TAG = "API-UTILS";

    public static <T> void getNetData(Context context, OnResponseData<T> responseData, Call<Result<T>> call){
        call.enqueue(new Callback<Result<T>>() {
            @Override
            public void onResponse(@NonNull Call<Result<T>> call, @NonNull Response<Result<T>> response) {
                if (response.isSuccessful()) {
                    Result<T> result = response.body();
                    if(result.getCode() == 200){
                        responseData.onData(result.getData());
                    }
                    else {
                        Toast.makeText(context,result.getMessage(),Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<Result<T>> call, Throwable t) {
                Toast.makeText(context,"网络错误",Toast.LENGTH_LONG).show();
            }
        });
    }

}
